<template>
	<view>
		<uni-row class="demo-uni-row">
			<uni-col :span="12">
				<button type="default" @click="setType('vin')" class="border-radius-no"
					:class="{'btn-red line-height-45 font-white':type == 'vin'}">VIN码</button>
			</uni-col>
			<uni-col :span="12">
				<button type="default" @click="setType('vehicle')" class="border-radius-no"
					:class="{'btn-red line-height-45 font-white':type == 'vehicle'}">行驶证</button>
			</uni-col>
		</uni-row>
		<view class="pad10">
			<!-- <h3>当前选择识别: {{type == "vin"? "VIN码" : "行驶证"}}</h3> -->
			<div class="clear h10"></div>
			<camera device-position="back" flash="off" @error="error" style="width: 100%; height: 240px;" v-if="showCamera">
				<cover-view>
					<cover-image :src=src class="scan-img"></cover-image>
				</cover-view>
			</camera>
		</view>
		<div class="clearfix bottom-btn">
			<button type="primary" style="width:50%" @click="takePhoto"
				class="btn-red border-radius-no fxl">OCR识别</button>
			<button type="primary" style="width:50%" @click="chooseImage"
				class="btn-red border-radius-no fxl ">从相册选择</button>
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showCamera: false,
				type: "vin",
				src: "/static/scan-vin.png"
			}
		},

		onShow() {
			let that = this

			uni.getSetting({
				success(res) {
					console.log(res.authSetting)
					
					if (!res.authSetting["scope.camera"]) {
						uni.authorize({
							scope: 'scope.camera',
							success() {
								that.showCamera = true
							},
							fail: function(error) {
								console.log(error)
								uni.showModal({
									title: '提示',
									content: '授权获取相机权限，用于识别行驶证',
									success: function(res) {
										if (res.confirm) {
											console.log('用户点击确定');
											uni.openSetting({
												success(res) {
													console.log(res.authSetting)
												},
												fail: function(error2) {
													console.log(error2)
												}
											})
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								})

							}
						})

					} else
						that.showCamera = true
				}
			})


		},


		methods: {
			setType(type) {
				this.type = type
				if (type == "vin")
					this.src = "/static/scan-vin.png"
				else
					this.src = "/static/scan-img.png"
			},

			chooseImage() {
				let that = this

				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						uni.showLoading({
							title: '识别中...'
						});
						uni.uploadFile({
							url: getApp().globalData.serviceUrl + "/chexk/kuybChexk/baiduOcrCarData",
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								'type': that.type
							},
							success: (uploadFileRes) => {
								uni.hideLoading()

								let ocrData = JSON.parse(uploadFileRes.data)
								if (!ocrData.success) {
									uni.hideLoading()
									uni.showToast({
										icon: 'none',
										title: ocrData.message
									})

									return
								}

								uni.request({
									url: getApp().globalData.serviceUrl +
										"/chexk/kuybChexk/queryChelxqByVin",
									data: {
										vin: ocrData.result.vinCode
									},
									header: {},
									method: 'GET',
									success: (resp) => {
										if (resp.data.success) {
											resp.data.result.vin = ocrData.result
												.vinCode
											if (ocrData.result.vehicleLicense !=
												null) {
												resp.data.result.chudrq = ocrData
													.result.vehicleLicense.fazrq
												resp.data.result.fadjh = ocrData.result
													.vehicleLicense.fadjhm
												resp.data.result.cheph = ocrData.result
													.vehicleLicense.haophm
												resp.data.result.chez = ocrData.result
													.vehicleLicense.suoyr
												resp.data.result.diz = ocrData.result
													.vehicleLicense.zhuz
											}

											uni.setStorage({
												key: 'carInfo',
												data: resp.data.result,
												success() {
													uni.navigateTo({
														url: "../baofShis/baofShis"
													})
												}
											})

										} else {
											uni.showToast({
												icon: 'none',
												title: resp.data.message
											})
										}
									}
								})
							}
						})
					}
				})
			},

			takePhoto() {
				let that = this
				const ctx = uni.createCameraContext();
				ctx.takePhoto({
					quality: 'low',
					success: (res) => {
						uni.showLoading({
							title: '识别中...'
						});

						uni.uploadFile({
							url: getApp().globalData.serviceUrl + "/chexk/kuybChexk/baiduOcrCarData",
							filePath: res.tempImagePath,
							name: 'file',
							formData: {
								'type': that.type
							},
							success: (uploadFileRes) => {
								let ocrData = JSON.parse(uploadFileRes.data)
								if (!ocrData.success) {
									uni.hideLoading();
									uni.showToast({
										icon: 'none',
										title: ocrData.message
									});

									return;
								}

								uni.request({
									url: getApp().globalData.serviceUrl +
										"/chexk/kuybChexk/queryChelxqByVin",
									data: {
										vin: ocrData.result.vinCode
									},
									header: {},
									method: 'GET',
									success: (resp) => {
										uni.hideLoading()

										if (resp.data.success) {
											resp.data.result.vin = ocrData.result
												.vinCode
											if (ocrData.result.vehicleLicense !=
												null) {
												resp.data.result.chudrq = ocrData
													.result.vehicleLicense.fazrq
												resp.data.result.fadjh = ocrData.result
													.vehicleLicense.fadjhm
												resp.data.result.cheph = ocrData.result
													.vehicleLicense.haophm
												resp.data.result.chez = ocrData.result
													.vehicleLicense.suoyr
												resp.data.result.diz = ocrData.result
													.vehicleLicense.zhuz
											}

											uni.setStorage({
												key: 'carInfo',
												data: resp.data.result,
												success() {
													uni.navigateTo({
														url: "../../pages2/baofShis/baofShis"
													})
												}
											})

										} else {
											uni.showToast({
												icon: 'none',
												title: resp.data.message
											})
										}
									}
								});
							}
						});
					}
				});

			},
			error(e) {
				console.log(e.detail);
			}
		}
	}
</script>

<style>
	.scan-img {
		opacity: 0.4;
		width: 100%;
		height: 500upx;
	}
</style>
